#pragma once

#include <iostream>
#include "Protocol.hpp"

enum
{
    Div_Zero,
    Mod_Zero,
    Other_Oper
};

class ServerCal
{
public:
    Response CalculatorHelper(Request& req)
    {
        Response resp(0,0);
        switch(req.op)
        {
            case '+':
                resp.result = req.x + req.y;
                break;
            case '-':
                resp.result = req.x - req.y;
                break;
            case '*':
                resp.result = req.x * req.y;
                break;
            case '/':
                if(req.y == 0) resp.code = Div_Zero;
                else resp.result = req.x / req.y;
                break;
            case '%': 
                if(req.y == 0) resp.code = Mod_Zero;
                else resp.result = req.x % req.y;
                break;
            default:
                resp.code = Other_Oper;
                break;
        }
       
        return resp;
    }

    std::string Calculator(std::string& package)
    {
        std::string content;
        bool res = Decode(package,&content);
        if(!res) return "";

        Request req;
        res = req.Deserialize(content);
        if(!res) return "";

        content = "";
        Response resp = CalculatorHelper(req);//result = 30, code = 0
        resp.Serialize(&content);//"30 0"
        content = Encode(content);// "len"\n"30 0

        return content;
    }

};